//
//  CPCollectionView.m
//  News
//
//  Created by chen on 15/3/25.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "CPCollectionView.h"
@interface CPCollectionView()


@end

@implementation CPCollectionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (UITableView *)leftView{
    if (_leftView == nil){
        
        for (id view in self.subviews) {
            if ( [view isKindOfClass:[UITableView class]])
            {
                _leftView = view;
                break;
            }
        }
        
    }
    return  _leftView;
    
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CGPoint currentP = [touch locationInView:self];
    CGPoint previousP  =  [touch  previousLocationInView:self];
    
    CGFloat width = currentP.x - previousP.x;
    self.totalWidth += width;
    
    CGFloat x =  self.leftView.frame.origin.x + width;
    CGFloat tWidth =self.leftView.frame.size.width;
        
    CGFloat tHeight = self.leftView.frame.size.height;
    
    if (self.totalWidth < tWidth)
    {
       
        self.leftView.frame = CGRectMake(x, 0.0f, tWidth, tHeight);
        
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super  touchesEnded:touches withEvent:event];
    
     CGFloat tWidth =self.leftView.frame.size.width;
    
     CGFloat tHeight = self.leftView.frame.size.height;
    if (self.totalWidth > tWidth * 0.5 ){
        
        [UIView animateWithDuration:0.5f animations:^{
           
            self.leftView.frame = CGRectMake(0.0f, 0.0f, tWidth, tHeight);
            
        }];
        self.totalWidth = tWidth;
        
    }else {
        
        [UIView animateWithDuration:0.5f animations:^{
            
            self.leftView.frame = CGRectMake(-tWidth, 0.0f, tWidth, tHeight);
            
        }];
        
        self.totalWidth = 0.0f;
    }
    

}




@end
